<?php
/**
 * Created by PhpStorm.
 * User: Eric
 * Date: 2021/10/16
 * Time: 13:37
 */

namespace app\admin\controller;

use app\BaseController;
use app\exception\HttpExceptions;
use app\libs\Token;
use app\model\Coin;
use app\validate\ManagerValidate;
use think\facade\Db;
use think\Request;
class Login extends BaseController
{
    public function login(Request $request) {
        $data = $request-> post();
        (new ManagerValidate())->checkParam($data);
        $manager = Db::name('manager')->where('mname', $data['mname'])->find();
        if($manager) {
            if(password_verify($data['password'],$manager['password'])){
                $manager = array_diff_key($manager,['password' => 'xxx']);
                $authorization = Token::generateToken($manager);
                return $this->result(['authorization' => $authorization]);
            }
            throw new HttpExceptions('密码错误');
        } else {
            throw new HttpExceptions('该账号不存在',404);
        }
    }
    public function download() {
        $activeCode = Coin::generateCode();
        return download($activeCode,'activeCode.txt', true);
    }
}